$(function () {

    // $(document)
    //     .off("click", "#import-file-btn")
    //     .on("click", "#import-file-btn", function () {
    //         let formData = new FormData($("#upload-form")[0]);
    //         $.ajax({
    //             url: "../product/importData",
    //             type: 'POST',
    //             data: formData,
    //             async: false,
    //             cache: false,
    //             contentType: false,
    //             processData: false,
    //             success: function (data) {
    //                 console.log(data);
    //             },
    //             error: function (data) {
    //                 alert("上传失败,请联系管理员");
    //             }
    //         });
    //     });

    $("#jqGrid").Grid({
        url: '../product/list',
        colModel: [
            {label: '产品名称', name: 'title', index: 'title', width: 80},
            {label: '银行', name: 'loanBankName', width: 80},
            {
                label: '最大额度',
                name: 'maxQuota',
                index: 'max_quota',
                width: 80,
                formatter: function (cellValue, option, cell) {
                    if (null == cellValue || undefined == cellValue || "" == cellValue) {
                        return "";
                    }
                    return cellValue + " 万元";
                }
            },
            {label: '贷款年限', name: 'years', index: 'years', width: 80},
            {
                label: '贷款利率',
                name: 'loanRate',
                index: 'loanRate',
                width: 80,
                formatter: function (cellValue, option, cell) {
                    if (null == cellValue || undefined == cellValue || "" == cellValue) {
                        return "";
                    }
                    return cellValue + "%";
                }
            }
        ]
    });

    $('#introduce').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '500px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../product/uploadFile',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        product: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空'}
            ]
        },
        show: {
            importData: false
        },
        options: {},
        q: {
            title: ''
        },
        chosenTaxArr: [],
        uploadFile: []
    },
    mounted: function () {
        let vmObj = this;
        Ajax.request({
            url: "../common/getOptionValues",
            type: "GET",
            contentType: "application/json",
            async: true,
            successCallback: function (r) {
                vmObj.options = r;
            }
        });
    },
    methods: {
        makeItHappen: function () {
            let formData = new FormData($("#upload-form")[0]);
            $.ajax({
                url: "../product/importData",
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    alert(data['msg']);
                    vm.reload();
                },
                error: function (data) {
                    alert("上传失败,请联系管理员");
                }
            });
        },
        uploadSuccess: function () {
        },
        importData: function () {
            vm.show.importData = true;
        },
        downloadExcel: function () {
            window.location.href = "https://oss-beemark-lht.oss-cn-beijing.aliyuncs.com/lst/excel/template.xlsx";
        },
        updateTaxLevel: function () {
            vm.product.taxLevel = vm.chosenTaxArr.join(",");
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.product = {};
            $('#introduce').editable('setHTML', '');
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let val = $("#introduce").editable('getHTML');
            vm.product.details = val;

            let url = vm.product.id == null ? "../product/save" : "../product/update";

            Ajax.request({
                url: url,
                params: JSON.stringify(vm.product),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../product/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../product/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.product = r.product;
                    let taxLevel = vm.product.taxLevel;
                    if (taxLevel != "") {
                        vm.chosenTaxArr = taxLevel.split(",");
                    }
                    $('#introduce').editable('setHTML', vm.product.details);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'title': vm.q.title},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                title: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});